package com.zky.gulimall.product.app;

import java.util.Arrays;
import java.util.Map;

import com.zky.gulimall.product.vo.SpuSaveVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.zky.gulimall.product.entity.SpuInfoEntity;
import com.zky.gulimall.product.service.SpuInfoService;
import com.zky.common.utils.PageUtils;
import com.zky.common.utils.R;


/**
 * spu信息
 *
 * @author zky
 * @email 574096634@qq.com
 * @date 2022-08-17 21:28:48
 */
@RestController
@RequestMapping("product/spuinfo")
public class SpuInfoController {
    @Autowired
    private SpuInfoService spuInfoService;


    /**
     * 商品上架
     */
    @PostMapping("/{spuId}/up")
    public R spuUp(@PathVariable("spuId") Long spuId) {
        try {
            spuInfoService.up(spuId);
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return R.error(e.getMessage());
        }
    }

    /**
     * 根据skuId返回spu信息
     */
    @GetMapping("/skuId/{skuId}")
    public R spuInfoBySkuId(@PathVariable("skuId") Long skuId) {
        SpuInfoEntity infoEntity = spuInfoService.spuInfoBySkuId(skuId);
        return R.ok().put("data", infoEntity);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        try {
            PageUtils page = spuInfoService.queryPageByContion(params);

            return R.ok().put("page", page);
        } catch (Exception e) {
            e.printStackTrace();
            return R.error(e.getMessage());
        }
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("product:spuinfo:info")
    public R info(@PathVariable("id") Long id) {
        SpuInfoEntity spuInfo = spuInfoService.getById(id);

        return R.ok().put("spuInfo", spuInfo);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:spuinfo:save")
    public R save(@RequestBody SpuSaveVo spuSaveVo) {
        spuInfoService.save(spuSaveVo);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:spuinfo:update")
    public R update(@RequestBody SpuInfoEntity spuInfo) {
        spuInfoService.updateById(spuInfo);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:spuinfo:delete")
    public R delete(@RequestBody Long[] ids) {
        spuInfoService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
